package com.Interface.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Interface.dao.ClassInfoMapper;
import com.Interface.dao.TopicInfoMapper;
import com.Interface.dao.TopicMarkInfoMapper;
import com.Interface.dao.UserInfoMapper;
import com.Interface.entity.TopicInfo;
import com.Interface.entity.TopicMarkInfo;

@Service
public class MarkInfoService {

	@Autowired
	TopicMarkInfoMapper topicMarkDao;
	
	@Autowired
	TopicInfoMapper topicInfoDao;
	
	@Autowired
	UserInfoMapper userDao;
	
	@Autowired
	ClassInfoMapper classInfoDao;
	
	public List<TopicMarkInfo> selectUserMarkByTopicAndType(Map<String, Object> param){
		return topicMarkDao.selectUserMarkByTopicAndType(param);
	}
	
	public List<Map<String, Object>> selectComment(Map<String, Object> param){
		return topicMarkDao.selectComment(param);
	}
	public  List<Map<String, Object>> getUserCommentbyUserId(Map<String, Object> param){
		return topicMarkDao.getUserCommentbyUserId(param);
	}
	
	public String topicMarkOperate(TopicMarkInfo topicMarkInfo){
		Map<String, Object> topicInfo=topicMarkDao.getMarkTopicInfo(topicMarkInfo);
		if(topicInfo==null){
			return "找不到对应话题";
		}else{
			if(topicMarkInfo.getType()!=4)
				topicMarkInfo.setClassId(Integer.parseInt(topicInfo.get("class_id").toString()));
			
			topicMarkInfo.setBackUserId(Integer.parseInt(topicInfo.get("user_id").toString()));
		}
		topicMarkInfo.setCreateTime(new Date());
		topicMarkInfo.setThumbsCnt(0);
		if (topicMarkInfo.getType()==7){  //给评论回复消息特殊处理
//			if(!topicMarkInfo.getBackUserId().equals(topicMarkInfo.getUserId())){
//				return  "只有作者能进行回复";
//			}
			TopicMarkInfo  tmpInfo=  topicMarkDao.selectByPrimaryKey(topicMarkInfo.getTopicMarkId());
			if(tmpInfo==null){
				return  "找不到对应的评论信息";
			}
			topicMarkInfo.setBackUserId(tmpInfo.getUserId());
			topicMarkInfo.setUserId(topicMarkInfo.getUserId());
			
		}
		int  rlt = topicMarkDao.insert(topicMarkInfo);
		  if(rlt<=0)
			  return  "操作失败";
		  
		if(topicMarkInfo.getType()==4){//转发
			TopicInfo info = topicInfoDao.selectByPrimaryKey(topicMarkInfo.getTopicId());
			info.setIsForward(0);
			info.setForwardCnt(topicMarkInfo.getTopicId());
			info.setForwordComment(topicMarkInfo.getComment());
			info.setClassId(topicMarkInfo.getClassId());
			info.setUserId(topicMarkInfo.getUserId());
			info.setIndroduce(topicMarkInfo.getComment()+"@"+info.getUserInfo().getName()+":"+info.getIndroduce());
			info.setReadCnt(0);
			info.setCommentCnt(0);
			info.setForwardCnt(0);
			info.setThumbsCnt(0);
			info.setCollectCnt(0);
			info.setMaxReadCntDay(0);
			info.setCurReadCntDay(0);
			info.setCreateTime(new Date());
			info.setId(null);
			rlt = topicInfoDao.insertSelective(info);
			if(rlt==1){
				 rlt = userDao.addTopicCntByUserId(topicMarkInfo.getUserId());	
				 rlt = classInfoDao.addClassTopicCnt(topicMarkInfo.getClassId());
			}
		}
		  
		if(topicMarkInfo.getType()==2){//文章点赞
			topicInfoDao.updateTopicThumbsCnt(topicMarkInfo.getTopicId());
		}else if(topicMarkInfo.getType()==3){//文章评论
			topicInfoDao.updateTopicCommentCnt(topicMarkInfo.getTopicId());
		}else if(topicMarkInfo.getType()==4){//文章转发
			topicInfoDao.updateTopicForwardCnt(topicMarkInfo.getTopicId());
		}else if(topicMarkInfo.getType()==5){//文章收藏
			topicInfoDao.updateTopicCollectCnt(topicMarkInfo.getTopicId());
			userDao.addColletionCntByUserId(topicMarkInfo.getUserId());
		}else if(topicMarkInfo.getType()==6){//评论点赞
			topicMarkDao.updateTopicCommentThumbsCnt(topicMarkInfo.getTopicMarkId());
		}

		return "";
	}
	public int deleteByCollection(Map<String, Object> param){
		int rlt = topicMarkDao.deleteByCollection(param); 
		
		if(rlt!=0){
			rlt= userDao.reduceColletionCntByUserId(Integer.parseInt(param.get("user_id").toString()));
			rlt= topicInfoDao.reduceTopicCollectCnt(Integer.parseInt(param.get("topic_id").toString()));
		}
		return rlt;
	}
	public int deleteTopicThumbs(Map<String, Object> param){
		int rlt = topicMarkDao.deleteTopicThumbs(param);
		if(rlt!=0){
		    topicInfoDao.reduceTopicThumbsCnt(Integer.parseInt(param.get("topic_id").toString()));
		}
		return rlt;
	}
	public int deleteCommentThumbs(Map<String, Object> param){
		int rlt = topicMarkDao.deleteCommentThumbs(param);
		if(rlt!=0){
			topicMarkDao.reduceTopicCommentThumbsCnt(Integer.parseInt(param.get("topic_mark_id").toString()));
		}
		return rlt;
	}
}
